#include "widget.h"
#include "ui_widget.h"
#include <QLabel>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_tabWidget_tabCloseRequested(int index)
{
    pq.push(hash[ui->tabWidget->widget(index)]);
    ui->tabWidget->removeTab(index);
}


void Widget::on_pushButton_clicked()
{
    static int number = 0;
    QWidget* w = new QWidget();
    QLabel* label = new QLabel(w);
    label->resize(100, 50);
    if(pq.empty())
    {
        hash[w] = number;
        label->setText("标签" + QString::number(number));
        ui->tabWidget->addTab(w, "Tab " + QString::number(number++));

    }
    else
    {
        int index = pq.top();
        hash[w] = index;
        pq.pop();
        label->setText("标签" + QString::number(index));
        ui->tabWidget->addTab(w, "Tab " + QString::number(index));
    }

    ui->tabWidget->setCurrentIndex(ui->tabWidget->count());
}


void Widget::on_pushButton_2_clicked()
{
    int index = ui->tabWidget->currentIndex();
    pq.push(hash[ui->tabWidget->widget(index)]);
    ui->tabWidget->removeTab(index);
}


void Widget::on_tabWidget_currentChanged(int index)
{
    qDebug() << "当前页为：" << index;
}

